package com.cjj.swagger.config;

import com.fasterxml.classmate.TypeResolver;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName SwaggerConfig
 * @Date 2021/11/03 13:41:22
 * @Created by BrownC_
 * @email ccc-ju@outlook.com
 */
@Configuration
@EnableSwagger2
//@EnableSwaggerBootstrapUI
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {

    @Value("${server.port:8080}")
    private String serverPort;

    @Bean
    public Docket createRestApi(TypeResolver typeResolver) throws UnknownHostException {
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        Arrays.stream(ResConst.values()).forEach(resConst -> responseMessageList.add(new ResponseMessageBuilder().code(resConst.getCode()).message(resConst.getMessage()).build()));
        return new Docket(DocumentationType.SWAGGER_2)
                .globalResponseMessage(RequestMethod.GET,responseMessageList)
                .globalResponseMessage(RequestMethod.POST,responseMessageList)
                .apiInfo(apiInfo())
                .host(InetAddress.getLocalHost().getHostAddress() + ":" + serverPort)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cjj"))
                .paths(PathSelectors.any()).build()
//                .alternateTypeRules(
//                        AlternateTypeRules.newRule(typeResolver.resolve(Callable.class,
//                                typeResolver.resolve(R.class, WildcardType.class)
//                                ),
//                                typeResolver.resolve(WildcardType.class)
//                        )
//                )
            ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("BrownC_接口文档")
                .description("user-operation")
                .termsOfServiceUrl("http://www.laobinggun.club/")
                .contact(new Contact("BrownC_", "http://www.laobinggun.club/", "ccc-ju@outlook.com"))
                .version("1.0.0")
                .build();
    }

}
